/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 9, 2011
 * @Createtime 11:45:51 AM
 */

package com.tmh.study.fMVW
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	import it.gotoandplay.smartfoxserver.SFSEvent;
	import it.gotoandplay.smartfoxserver.SmartFoxClient;
	import it.gotoandplay.smartfoxserver.data.Room;
	import it.gotoandplay.smartfoxserver.data.User;

	public class Ch05_02 extends MovieClip
	{
		private var _sfs:SmartFoxClient;

		private var _avatarList:Array = new Array();

		public function Ch05_02()
		{
			_sfs = new SmartFoxClient(true);

			_sfs.addEventListener(SFSEvent.onConnection, onConnection);
			_sfs.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdate);
			_sfs.addEventListener(SFSEvent.onJoinRoom, onJoinRoom);
			_sfs.addEventListener(SFSEvent.onUserVariablesUpdate, onUserVariablesUpdate);
			_sfs.addEventListener(SFSEvent.onUserEnterRoom, onUserEnterRoom);
			_sfs.addEventListener(SFSEvent.onUserLeaveRoom, onUserLeaveRoom);

			_sfs.connect("127.0.0.1", 9339);
		}

		private function drawAvatarAtRandomPlace(username:String):Avatar
		{
			var avatar:Avatar = new Avatar();
			addChild(avatar);
			
			avatar.nameText.text = username;
			
			/* random position with 50 pixels margin to the border */
			avatar.x = Math.random() * (stage.stageWidth - 100) + 50;
			avatar.y = Math.random() * (stage.stageHeight - 100) + 50;
			
			return avatar;
		}

		private function drawAvatar(username:String, posX:int, posY:int):Avatar
		{
			var avatar:Avatar = new Avatar();
			addChild(avatar);
			
			avatar.nameText.text = username;
			
			avatar.x = posX;
			avatar.y = posY;
			
			return avatar;
		}

		private function onJoinRoom(e:SFSEvent):void
		{
			//var myAvatar:Avatar = drawAvatarAtRandomPlace(_sfs.myUserName);
			var myAvatar:Avatar = drawAvatarAtRandomPlace("me ^_^");
			myAvatar.nameText.textColor = 0xcc0000;

			/* save position of myself into user variables */
			var params:Object = new Object();
			params.posX = myAvatar.x;
			params.posY = myAvatar.y;
			params.firstTime = true;
			
			_sfs.setUserVariables(params);

			/* get current room data */
			var room:Room = e.params.room;
			
			/* load all users and draw, including myself */
			var userList:Array = room.getUserList();

			for each (var user:User in userList)
			{
				/* exclude myself */
				if (user.getName() != _sfs.myUserName)
				{
					/* get user's position from user variables */
					var posX:int = user.getVariable('posX');
					var posY:int = user.getVariable('posY');
					var avatar:Avatar = drawAvatar(user.getName(), posX, posY);
					
					_avatarList[user.getName()] = avatar;
				}
			}
		}

		private function onUserEnterRoom(e:SFSEvent):void
		{
		}
		
		private function onUserVariablesUpdate(e:SFSEvent):void
		{
			/* getting who has updated the variable */
			var user:User = e.params.user;
			var username:String = user.getName();

			/* this is a new user, we create a new avatar movieclip */
			if (user.getVariable('firstTime') == true)
			{
				var posX:int = user.getVariable('posX');
				var posY:int = user.getVariable('posY');
				var avatar:Avatar = drawAvatar(username, posX, posY);
				
				_avatarList[username] = avatar;
			}
		}

		private function onUserLeaveRoom(e:SFSEvent):void
		{
			/* know which user is leaving the room */
			var username:String = e.params.userName;
			
			removeChild(_avatarList[username]);
		}

		private function onConnection(e:SFSEvent):void
		{
			var ok:Boolean = e.params.success;

			if (ok)
			{
				_sfs.login("simpleChat", "", "");
			}
		}

		private function onRoomListUpdate(e:SFSEvent):void
		{
			_sfs.autoJoin();
		}
	}
}